package com.swiot.biz.controller;

import com.swiot.biz.constants.TemplateConstants;
import com.swiot.biz.domain.TemplateData;
import com.swiot.biz.service.ITemplateDataService;
import com.swiot.common.annotation.Log;
import com.swiot.common.core.controller.BaseController;
import com.swiot.common.core.domain.AjaxResult;
import com.swiot.common.core.page.TableDataInfo;
import com.swiot.common.enums.BusinessType;
import com.swiot.common.utils.poi.ExcelUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 模板数据Controller
 *
 * @author kingdom
 * @date 2025-09-04
 */
@RestController
@RequestMapping("/biz/data")
public class TemplateDataController extends BaseController {
    @Autowired
    private ITemplateDataService templateDataService;

    /**
     * 查询模板数据列表
     */
    @PreAuthorize("@ss.hasPermi('biz:data:list')")
    @GetMapping("/list")
    public TableDataInfo list(TemplateData templateData) {
        // 设置默认查询条件，提高查询效率
        if (templateData == null) {
            templateData = new TemplateData();
        }

        startPage();
        List<TemplateData> list = templateDataService.selectTemplateDataPageList(templateData);
        return getDataTable(list);
    }

    /**
     * 导出模板数据列表
     */
    @PreAuthorize("@ss.hasPermi('biz:data:export')")
    @Log(title = "模板数据", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TemplateData templateData) {
        List<TemplateData> list = templateDataService.selectTemplateDataList(templateData);
        ExcelUtil<TemplateData> util = new ExcelUtil<TemplateData>(TemplateData.class);
        util.exportExcel(response, list, "模板数据数据");
    }

    /**
     * 获取模板数据详细信息
     */
    @PreAuthorize("@ss.hasPermi('biz:data:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return success(templateDataService.selectTemplateDataById(id));
    }

    /**
     * 新增模板数据
     */
    @PreAuthorize("@ss.hasPermi('biz:data:add')")
    @Log(title = "模板数据", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TemplateData templateData) {
        // 参数检查
        if (StringUtils.isEmpty(templateData.getTemplateId())) {
            return error(TemplateConstants.ErrorMessages.TEMPLATE_ID_EMPTY);
        }

        if (StringUtils.isEmpty(templateData.getReportDate())) {
            return error(TemplateConstants.ErrorMessages.REPORT_DATE_EMPTY);
        }

        if (templateData.getDeptId() == null) {
            return error(TemplateConstants.ErrorMessages.DEPT_ID_EMPTY);
        }

        // 设置发布状态为未发布
        if (templateData.getReportDate().lastIndexOf("00") > -1) {
            templateData.setDataType(TemplateConstants.DataType.BUDGET);
        }
        templateData.setTemplatePublishStatus(TemplateConstants.PublishStatus.UNPUBLISHED);

        return toAjax(templateDataService.insertTemplateData(templateData));
    }

    /**
     * 修改模板数据
     */
    @PreAuthorize("@ss.hasPermi('biz:data:edit')")
    @Log(title = "模板数据", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TemplateData templateData) {
        // 基础判空检查
        if (templateData == null) {
            return error("模板数据不能为空");
        }

        if (StringUtils.isEmpty(templateData.getId())) {
            return error("模板数据ID不能为空");
        }

        return toAjax(templateDataService.updateTemplateData(templateData));
    }

    /**
     * 删除模板数据
     */
    @PreAuthorize("@ss.hasPermi('biz:data:remove')")
    @Log(title = "模板数据", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {

        return toAjax(templateDataService.deleteTemplateDataByIds(ids));
    }

    /**
     * 发布模板数据
     */
    @PreAuthorize("@ss.hasPermi('biz:data:publish')")
    @Log(title = "模板数据", businessType = BusinessType.UPDATE)
    @PutMapping("/publish/{id}")
    public AjaxResult publish(@PathVariable("id") String id) {
        return toAjax(templateDataService.publishTemplateData(id));
    }

    /**
     * 撤销模板数据
     */
    @PreAuthorize("@ss.hasPermi('biz:data:unpublish')")
    @Log(title = "模板数据", businessType = BusinessType.UPDATE)
    @PutMapping("/unpublish/{id}")
    public AjaxResult unpublish(@PathVariable("id") String id) {
        return toAjax(templateDataService.unpublishTemplateData(id));
    }


}
